// pages/nearMap/nearMap.js
var app=getApp();
var util=require("../../utils/util");
var home=require("../../api/home");


Page({
  /*页面的初始数据*/
  data: {
    height: wx.getSystemInfoSync().windowHeight,
    address_info: '',
    mech_list: [],
    markers: [],
    marker_info: '',
    mark_index: 0,
    pop_show: true,
    ju_list: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
    value: [1],
    selectValue: 1,
    range: 0,
    currentPage: 1,
    pageSize: 10,
    hasMoreData: true,
    latitude: 22.80536,
    longitude: 113.29321,
    load_more: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'city',
      success:function(res){
        that.setData({address_info:res.data,latitude:res.data.lat,longitude:res.data.lng})
        var scope=wx.getStorageSync('scope')?wx.getStorageSync('scope'):0;
        //
        that.get_mech_list(res.data,scope);
      }
    })
    // 事件统计
    app.td_app_sdk.event({
      id: 'mechMap',
      label: '机构地图',
      params: {
        key1: "附近机构"
      }
    })
  },
  update_map:function(e){
    this.setData({load_more:true})
  },
  // map视野变化
  change_map:function(e){
    var that=this,info=that.data.address_info,range=that.data.range,load_more=that.data.load_more,scale1=16,scale2=0,hasMoreData=that.data.hasMoreData;
    if(load_more){
      if(e.detail.type=="end"&&(e.causedBy=="scale"||e.causedBy=="drag")){
        if(hasMoreData){
          that.get_mech_list(info,range)
        }
        // this.mapCtx = wx.createMapContext("map");
        // this.mapCtx.getScale({
        //   success:function(res){
        //     scale2=res.scale
        //   }
        // })
        // this.mapCtx.getCenterLocation({
        //   type: 'gcj02',
        //   success: function (res) {
        //     console.log(res)
        //     var scope=util.get_distance(that.data.latitude,that.data.longitude,res.latitude,res.longitude),rand=0.5;
        //     if(scale2<scale1&&scale2!=0){
        //       rand=rand*((scale1-scale2)*2)
        //     }
        //     if(scale2>scale1){
        //       rand=rand/((scale2-scale1)*2)
        //     }
        //     if(scope>rand){
        //       that.get_mech_list(info,range)
        //     }
        //   }
        // })
      }
    }
  },
  // 点击标记点
  markertap: function (e) {
    var that = this,markers=that.data.markers,info='';
    var id = e.detail.markerId;
    markers.forEach(item => {
      if(id==item.id&&id!="id"){
        info=item
      }
    })
    if(info){
      that.setData({marker_info: info})
    }
  },
  // 跳转
  nav_mech:function(){
    var that=this,info=that.data.marker_info;
    wx.navigateTo({
      url: '../mechDetail/mechDetail?id='+info.id+'&status=6',
    })
  },
  // 选择范围
  picker_change:function(e){
    var index = e.detail.value[0];
    this.setData({selectValue: index})
  },
  open_check:function(){
    this.setData({pop_show: !this.data.pop_show})
  },
  close_pop:function(){
    this.setData({pop_show: !this.data.pop_show})
  },
  confirm_scope:function(){
    var that=this,info=that.data.address_info,index=that.data.selectValue,ju_list=that.data.ju_list;
    that.setData({range:ju_list[index]})
    //
    that.get_mech_list(info,ju_list[index])
    that.close_pop()
  },
  return:function(){},
  // 获取附近机构列表
  get_mech_list:function(info,range){
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    wx.showLoading({title: '加载中'})
    var params={
      "lat": info.lat,
      "lng": info.lng,
      "currentPage": currentPage,
      "pageSize": pageSize,
      "province": info.province,
      "city": info.city,
      "region": info.district,
      "status": 2,
      "categoryParentNames": '',
      "name": '',
      "range": range>0?(range*1000):''
    }
    home.get_mech_list(params).then(res=>{
      if(res.data.code==200){
        var list = res.data.data.records,markers=[]
        var mech_list = that.data.mech_list;
        mech_list = util.extend(mech_list, list)
        if (list.length < pageSize) {
          that.setData({hasMoreData: false})
        } else {
          that.setData({hasMoreData: true,currentPage: currentPage + 1})
        }
        for(var i=0;i<mech_list.length;i++){
          var data={};
          data.id=mech_list[i].id;
          data.name=mech_list[i].name;
          data.isCertified=mech_list[i].isCertified;
          data.categoryNames=mech_list[i].categoryNames;
          data.score=mech_list[i].score?parseFloat(mech_list[i].score).toFixed(1):'5.0';
          data.distance=mech_list[i].distance;
          data.space=mech_list[i].distance>1000?(mech_list[i].distance/1000).toFixed(2):mech_list[i].distance;
          data.latitude=mech_list[i].lat;
          data.longitude=mech_list[i].lng;
          data.iconPath=mech_list[i].logoImg;
          data.width=48;
          data.height=48;
          markers.push(data)
        }
        var marker={
          id: "id",
          iconPath: '/images/icon32.png',
          latitude: info.lat,
          longitude: info.lng,
          width: 48,
          height: 48
        }
        markers.push(marker);
        that.setData({markers: markers,mech_list:mech_list,marker_info:markers[0]})
      };
    })
  },
})